/*
 * ReadStateMonitor.java
 * 
 * Created on Jul 24, 2007, 11:14:58 AM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mbari.vcr.timer;

import org.mbari.util.IObserver;
import org.mbari.vcr.IVCR;
import org.mbari.vcr.IVCRState;
import org.mbari.vcr.VCRAdapter;

/**
 *
 * @author brian
 */
public class StatusMonitor extends Monitor {
    
    public static final String MONITOR_NAME = "StatusMonitor";

    private IObserver stateObserver = new IObserver() {
        public void update(final Object obj, final Object changeCode) {
            IVCRState vcrState = (IVCRState) obj;
            if (vcrState.isConnected()) {
                start();
            } else {
                stop();
            }
        }
    };
    
    public StatusMonitor() {
        this(new VCRAdapter());
        setIntervalMin(250);
        setInterval(1000);
    }

    public StatusMonitor(final IVCR vcr) {
        super(ReadStateTimerTask.class, MONITOR_NAME, vcr);
    }
    
    @Override
    public synchronized void setVcr(final IVCR vcr) {
        IVCR oldVcr = getVcr();
        if (oldVcr != null) {
            oldVcr.getVcrState().removeObserver(stateObserver);
        }
        vcr.getVcrState().addObserver(stateObserver);
        super.setVcr(vcr);
    }

}
